home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 15764 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.5 KB

  1. Path: mayne.ugrad.cs.ubc.ca!not-for-mail
  2. From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: strncpy() ?
  5. Date: 21 Apr 1996 11:03:40 -0700
  6. Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
  7. Message-ID: <4ldt9sINNm1i@mayne.ugrad.cs.ubc.ca>
  8. References: <4ldkvd$553@venus.compulink.gr>
  9. NNTP-Posting-Host: mayne.ugrad.cs.ubc.ca
  10.  
  11. In article <4ldkvd$553@venus.compulink.gr>,
  12. Costas Vlassis <lonewolf@athena.compulink.gr> wrote:
  13.  >
  14.  >    Well I have this question, let's say we have the following :
  15.  >
  16.  >char alfa[40] = "1123";
  17.  >char beta[20];
  18.  >char gama[20];
  19.  >
  20.  >With strncpy(beta,alfa,2) beta = "11" that's fine but HOW can I make 
  21.  
  22. Note that the above strncpy will _not_ null terminate the characters added to
  23. beta. Unless you are sure that beta[2] contains a zero, the result is not a
  24. string. Then again, you might not care, but it's just a caveat.
  25.  
  26.  >gama = "23" that is the last 2 characters of alfa or even "3" the last 
  27.  >character... Is there a function that takes start and end as strings ?
  28.  >
  29.  >what I want is something like :
  30.  >
  31.  >strncpy2 (beta,alfa,2,4);
  32.  >
  33.  >    is this possible ?
  34.  
  35.     strncpy(beta,alfa+2,2);
  36.  
  37. The expression ``alfa'' has a value that is the pointer to the first character
  38. of the array alfa, or alfa[0]. You can involve this pointer value in pointer
  39. arithmetic, as I did above by adding a displacement of 2 to it. You could even
  40. do:
  41.  
  42.     strncpy(beta+3,alfa+2,2);
  43.  
  44. which would copy alfa[2] and alfa[3] into beta[3], beta[4].
  45.